// pages/chkdge/chkdge.js
import {request} from '../../request/index.js'
import regeneratorRuntime, { async } from '../../lib/runtime/runtime';
var QQMapWX = require("../../utils/qqmap-wx-jssdk.min");
var qqmapsdk;
import Notify from '../../vant/dist/notify/notify'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 消除隐患
    chkdgeForm:[],
    Ifchkdge1: true,
    Ifchkdge: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      key: 'TUHBZ-ZPFKU-XQAVA-BC4M4-ER4IZ-BXBLL'
    });
    this.getchkdgeForm(false)
  },

  ToSee: function(event){
    var lat = event.currentTarget.dataset.dgelat
    var lng = event.currentTarget.dataset.dgelng

    console.log(lat,lng)
    this.jingWeiduToDiZhi(lat,lng)
  },

  // 经纬度转换成地址
jingWeiduToDiZhi(latitude,longitude) {
  // 调用接口
  qqmapsdk.reverseGeocoder({
    location: {
      latitude: latitude ? latitude : null,
      longitude: longitude ? longitude : null
    },
    coord_type: 3, //baidu经纬度
    success: function(res){
      Notify({ type: 'primary', message: res.result.address });
    },
    fail: function(error) {
      console.log("err")
      console.error(error);
    },
    complete: function(res) {
      // console.log(res);
    }
  })
},
 /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // qqmapsdk = new QQMapWX({
    //   key: 'TUHBZ-ZPFKU-XQAVA-BC4M4-ER4IZ-BXBLL'
    // });
    this.getchkdgeForm(true);
    console.log(1)
  },
 async getchkdgeForm(refresh) {
  const res = await request({
    url: '/danger/findByJudge/' + wx.getStorageSync('empId'),
    header:{
      'Authorization':'Bearer '+wx.getStorageSync('token'),
    },
    method: "get",
  })
  console.log(res.data)
  if (res.data.status !== 200){
    var title = res.data.msg
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 2000
    })
    if (title === '您还未登录'){
      wx.reLaunch({
        url: '/pages/index/index',
      })
    }
  }
  else {
    if (! refresh) {
      wx.showLoading({
       title: '拼命加载中',
     })
   }
    var chkdgeForm = res.data.data.reverse()
    var chkdge = []
    if (chkdgeForm[0]) {
      console.log(chkdgeForm[0])
      chkdgeForm.forEach(function(item) {
        var infos = (item.dgeInfo+ "").split('imgGroup:')[0]
        var info = (infos === 'null') ? '暂无隐患信息' : infos
        var imgstr = (item.dgeInfo+ "").split('imgGroup:')[1]
        var arr = []
        if(imgstr) {
            arr = (imgstr+ "").split(",")
        }
        else {
            arr = ["null"]
        }
       chkdge.push({item,info: info,img:arr})
      });
      if (refresh) {
         this.setData({
          chkdgeForm: chkdge,
          Ifchkdge: false
        },wx.stopPullDownRefresh())
      }
      else{
        this.setData({
          chkdgeForm: chkdge,
          Ifchkdge: false
        },wx.hideLoading())
      }
    }
    else{
      if (refresh) {
         this.setData({
          Ifchkdge1: false
        },wx.stopPullDownRefresh({
          success: (res) => {},
        }))
      }
      else {
        this.setData({
          Ifchkdge1: false
        },wx.hideLoading())
      }
      // 没有消除记录
    }
  }
 }

})